<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                template="../../template/tagedu.xhtml">

    <ui:define name="title">
        <title><h:outputText value="TAG - Cadastro Unidade"/></title>
        <f:metadata>
            <f:viewParam name="organization" value="#{unityRegistration.organizationID}"/>
            <f:viewParam name="type" value="#{unityRegistration.organizationType}"/>
        </f:metadata>
        <link rel="Stylesheet" type="text/css" href="../../resources/css/unityRegistration.css"></link>
    </ui:define>
    <ui:define name="content">
        <f:event type="preRenderComponent" listener="#{unityRegistration.initialize()}"/>
        <h:outputText styleClass="organizationName" value="Cadastro de #{unityRegistration.getOrganizationName()}"/> 
        <h:form id="unityRegistrationForm">            
            <p:messages/>
            
            <p:tabView styleClass="unityRegistrationView" id="unityRegistrationView">
                <p:tab title="Dados Principais">
                    <p:panel>
                        <h:panelGrid id="personal" columns="2" styleClass="panelGrids">
                            <h:outputLabel for="unityFather" value="#{unityRegistration.organizationFather}:" rendered="#{unityRegistration.neededSetFather}"/>
                            <h:selectOneMenu required="true" requiredMessage="'#{unityRegistration.organizationFather}' é obrigatório" rendered="#{unityRegistration.neededSetFather}"
                                             id="unityFather" value="#{unityRegistration.unityFatherID}">
                                <f:selectItems value="#{unityRegistration.unitiesFather}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="name" value="Nome*:"/>
                            <p:inputText styleClass="name" id="name" value="#{unityRegistration.unity.name}" required="true" requiredMessage="'Nome' é obrigatório"/>

                            <h:outputLabel for="period" value="Período:"/>
                            <h:selectOneMenu id="period" value="#{unityRegistration.periodID}">
                                <f:selectItems value="#{unityRegistration.periodList}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="capacity" value="Capacidade:"/>
                            <p:inputText id="capacity" value="#{unityRegistration.unity.capacity}"/>

                            <h:outputLabel for="activated" value="Ativação*:"/>
                            <p:inputMask id="activated" mask="99/99/9999" value="#{unityRegistration.unity.activated.time}" 
                                         required="true" requiredMessage="'Ativação' é obrigatório">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </p:inputMask>

                            <h:outputLabel value="Sem data de desativação:" for="deactivatedIsNull"/>
                            <h:selectBooleanCheckbox id="deactivatedIsNull" value="#{unityRegistration.deactivatedIsNull}">
                                <f:ajax execute="deactivatedIsNull" render="deactivatedGroup"/>
                            </h:selectBooleanCheckbox>  
                        </h:panelGrid>    
                        <h:panelGrid columns="2" id="deactivatedGroup">
                            <h:outputLabel id="deactivatedLabel" for="deactivated" rendered="#{not unityRegistration.deactivatedIsNull}" value="Desativação:"/>
                            <p:inputMask id="deactivated" rendered="#{not unityRegistration.deactivatedIsNull}" 
                                         mask="99/99/9999" value="#{unityRegistration.unity.deactivated.time}"
                                         required="#{not unityRegistration.deactivatedIsNull}" requiredMessage="'Desativação' é obrigatório">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </p:inputMask>

                        </h:panelGrid>
                        <br/><br/>
                    </p:panel>
                </p:tab>
                <p:tab title="Dados Jurídicos" rendered="#{unityRegistration.needJuridic}">
                    <p:panel styleClass="juridicTab">
                        <h:panelGrid columns="2">
                            <h:outputLabel for="companyName" value="Nome Jurídico*:"/>
                            <p:inputText id="companyName" value="#{unityRegistration.juridic.companyName}" 
                                         required="true" requiredMessage="'Nome Jurídico' é obrigatório" styleClass="name"/>

                            <h:outputLabel for="fancyName" value="Nome Fantasia*:"/>
                            <p:inputText id="fancyName" value="#{unityRegistration.juridic.fancyName}" 
                                         required="true" requiredMessage="'Nome Fantasia' é obrigatório" styleClass="name"/>

                            <h:outputLabel for="birthday" value="Data de Fundação*:"/>
                            <p:inputMask id="birthday" mask="99/99/9999" value="#{unityRegistration.juridic.birthday.time}" 
                                         required="true" requiredMessage="'Data de Fundação' é obrigatório">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </p:inputMask>

                            <h:outputLabel for="phone" value="Telefone:"/>
                            <p:inputMask id="phone" mask="(99)9999-9999" value="#{unityRegistration.juridic.phone}"/>
                            <h:message styleClass="message" for="phone"/>
                        </h:panelGrid>  
                        <br/>
                        <h:outputLabel styleClass="specificData" value="Dados Específicos:"/>
                        <ui:repeat value="#{unityRegistration.additionalInfoList}" var="additionalInfo">
                            <h:panelGrid columns="2">
                                <h:outputLabel value="#{additionalInfo.additionalInfoType.acronym}*: "/>
                                <p:inputMask id="addInfoMask" mask="#{additionalInfo.additionalInfoType.mask}" 
                                             value="#{additionalInfo.value}" required="#{additionalInfo.additionalInfoType.compulsory}" 
                                             requiredMessage="'#{additionalInfo.additionalInfoType.acronym}' é obrigatório"/>                                
                            </h:panelGrid>
                        </ui:repeat>
                    </p:panel>
                </p:tab>
                <p:tab title="Endereço" rendered="#{unityRegistration.needJuridic}">
                    <p:panel id="address" styleClass="addressPanel">
                        <p:message for="cep"/>
                        <h:outputLabel value="CEP" for="cep"/>
                        <p:inputMask id="cep" value="#{unityRegistration.cep}" mask="99999999" required="true" requiredMessage="'CEP' é obrigatório"/>
                        <p:commandButton onstart="statusDialog.show();" onsuccess="statusDialog.hide();" value="Buscar" ajax="true" action="#{unityRegistration.searchCEP()}" update="address" process="@this, cep"/>
                        <br/>
                        <h:panelGroup id="zipcode">
                            <h:outputLabel value="Estado:" for="state"/>
                            <p:inputText id="state" value="#{unityRegistration.zipcode.city.state.name}" required="true" readonly="true"/>
                            <br/>
                            <h:outputLabel value="Cidade:" for="city"/>
                            <p:inputText id="city" value="#{unityRegistration.zipcode.city.name}" required="true" readonly="true"/>
                            <br/><br/>
                        </h:panelGroup>
                        <h:panelGroup rendered="#{unityRegistration.showAddress}">
                            <h:outputLabel value="Bairro:" for="neighborhood"/>
                            <p:autoComplete id="neighborhood" value="#{unityRegistration.neighborhood}" 
                                            completeMethod="#{unityRegistration.neighborhoodComplete}" 
                                            required="true" requiredMessage="'Bairro' é campo obrigatório"
                                            minQueryLength="3"/>
                            <br/>
                            <h:outputLabel value="Logradouro:" for="street"/>
                            <p:autoComplete id="street" value="#{unityRegistration.street}" 
                                            completeMethod="#{unityRegistration.streetComplete}" 
                                            required="true" requiredMessage="'Logradouro' é campo obrigatório"
                                            minQueryLength="3"/>
                            <br/><br/>
                            <h:outputLabel value="Número:" for="number"/>
                            <p:inputText id="number" value="#{unityRegistration.address.number}"/>
                            <br/>
                            <h:outputLabel value="Complemento" for="complement"/>
                            <p:inputText id="complement" value="#{unityRegistration.address.complement}"/>                            
                        </h:panelGroup>
                    </p:panel>
                </p:tab>                
            </p:tabView>
            
            <div class="buttonGroup">
                <p:commandButton ajax="true" action="#{unityRegistration.save()}" 
                                 value="Cadastrar" 
                                 process="@form" 
                                 update="@form" 
                                 onstart="statusDialog.show();" onerror="statusDialog.hide();"
                                 onsuccess="statusDialog.hide();">
                </p:commandButton>
            </div>
        </h:form>        
        
    </ui:define>

    <ui:define name="modals">
        <p:dialog modal="true" widgetVar="statusDialog" 
                  draggable="false" closable="false">
            <p:graphicImage value="../../resources/images/ajax/ajaxloader.gif"/>
        </p:dialog>
    </ui:define>
</ui:composition>